package com.felix.fund;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Fund {

    public static void main(String[] args) {
//        updateFundName();
        updateFundJZ();
        updateFundgz();
    }

    /**
     * update fund name
     */
    public static void updateFundName(){
        String sql = "SELECT code FROM fund";
        String columnName = "code";
        List<String> l = DBConnUtils.getColumn(sql, columnName);
        String url = "";
        for (String s:l) {
            url = "http://fundgz.1234567.com.cn/js/" + s + ".js";
            Map<String, Object> map = HttpClientUtils.fundApi(url);

            String updateSql = "UPDATE fund t SET t.name=? WHERE t.code=?";
            List<String> update_l = new ArrayList<String>();
            update_l.add(map.get("name").toString());
            update_l.add(map.get("fundcode").toString());
            String result = DBConnUtils.updateData(updateSql, update_l);
            System.out.println(result);
        }
        System.out.println("update fund name is ok...");
    }

    public static void updateFundgz(){
        String sql = "SELECT code FROM fund";
        String columnName = "code";
        List<String> l = DBConnUtils.getColumn(sql, columnName);
        String url = "";
        for (String s:l) {
            url = "http://fundgz.1234567.com.cn/js/" + s + ".js";
            Map<String, Object> map = HttpClientUtils.fundApi(url);

            String updateSql = "UPDATE fund t SET t.forecast_date=NOW(),t.forecast_net_worth=?,forecast_growth_rase=? WHERE t.`code`=?";
            List<String> update_l = new ArrayList<String>();
            update_l.add(map.get("gsz").toString());
            update_l.add(map.get("gszzl").toString());
            update_l.add(map.get("fundcode").toString());
            String result = DBConnUtils.updateData(updateSql, update_l);
            System.out.println(result);
        }
        System.out.println("update fund name is ok...");
    }
    /**
     * update fund jz
     */
    public static void updateFundJZ(){
        String sql = "SELECT code FROM fund";
        String columnName = "code";
        List<String> l = DBConnUtils.getColumn(sql, columnName);
        String url = "";
        for (String s:l) {
            url = "http://fundgz.1234567.com.cn/js/" + s + ".js";
            Map<String, Object> map = HttpClientUtils.fundApi(url);
            List<String> queryList = new ArrayList<String>();

            //1.根据api接口结果更新fund表对于数据列
            sql = "UPDATE fund t SET t.date=?,t.net_worth=? WHERE t.`code`=? AND t.date!=?";
            queryList.clear();
            queryList.add(map.get("jzrq").toString());
            queryList.add(map.get("dwjz").toString());
            queryList.add(map.get("fundcode").toString());
            queryList.add(map.get("jzrq").toString());
            DBConnUtils.updateData(sql, queryList);

//            SELECT * FROM fund_jz_details WHERE fund_code='007301' AND jzrq='2021-04-01';
//            INSERT INTO fund_jz_details(fund_code,jzrq,jz) VALUES('007301','2021-04-01','1.8222')
            //2.查下净值明细表是否存在数据
            sql = "SELECT fund_code FROM fund_jz_details WHERE fund_code=? AND jzrq=?";
            queryList.clear();
            queryList.add(map.get("fundcode").toString());
            queryList.add(map.get("jzrq").toString());
            List<String> fund_jz_details_l = DBConnUtils.getData(sql, "fund_code", queryList);

            if(fund_jz_details_l==null || fund_jz_details_l.size()==0){
                //3.步骤2的查下没有数据情况下新增一条记录
                sql = "INSERT INTO fund_jz_details(fund_code,jzrq,jz) VALUES(?,?,?)";
                queryList.clear();
                queryList.add(map.get("fundcode").toString());
                queryList.add(map.get("jzrq").toString());
                queryList.add(map.get("dwjz").toString());
                DBConnUtils.updateData(sql, queryList);
            }
        }
        System.out.println("update fund name is ok...");
    }
}
